package com.xiaolin.Thread;

/**
 * @ClassName TestThreadLocal
 * @Description TODO
 * @Detail 验证threadLocal是否是每个线程独有的
 *              结果：
 *                  每个线程都有一份副本
 * @Author MyPC
 * @Date 2020/9/26
 * @Version 1.0
 */
public class TestThreadLocal {
    static ThreadLocal<String> threadLocal=new ThreadLocal<>();
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("t1");
                try {
                    //睡眠，验证threadLocal是否是每个线程独有的，如果是线程共享的，则该值会被其他线程覆盖
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1睡眠之后获取：" + threadLocal.get());
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("t2");
                System.out.println("线程2先获取：" + threadLocal.get());
            }
        });
        t1.start();
        t2.start();

    }
}
